package com.example.demo.controller;

import java.text.DateFormat;
import java.util.Date;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.handler.annotation.SendTo;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Controller;

import com.example.demo.model.Message;

@Controller
@EnableScheduling
public class DouniuController {
	
	@Resource
	private DateFormat dateFormat;
	
	 @Autowired
	 private SimpMessagingTemplate messagingTemplate;
	
	@MessageMapping("/app/send")
	@SendTo("/topic/send")
	public Message send(Message msg) {
		msg.setDate(dateFormat.format(new Date()));
		return msg;
	}
	
	@Scheduled(fixedRate=1000)
	public void ping() {
		messagingTemplate.convertAndSend("/topic/ping",dateFormat.format(new Date()));
	}
	
}
